package com.nbsaas.boot.service.impl;

import com.nbsaas.boot.entity.KnowledgePoint;
import com.nbsaas.boot.repository.KnowledgePointRepository;
import com.nbsaas.boot.service.KnowledgePointService;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@RequiredArgsConstructor
public class KnowledgePointServiceImpl implements KnowledgePointService {
    
    private final KnowledgePointRepository knowledgePointRepository;

    @Override
    @Transactional
    public KnowledgePoint createKnowledgePoint(KnowledgePoint knowledgePoint) {
        return knowledgePointRepository.save(knowledgePoint);
    }

    @Override
    @Transactional
    public KnowledgePoint updateKnowledgePoint(Long id, KnowledgePoint knowledgePoint) {
        KnowledgePoint existingKnowledgePoint = knowledgePointRepository.findById(id)
            .orElseThrow(() -> new RuntimeException("知识点不存在"));
        
        existingKnowledgePoint.setTitle(knowledgePoint.getTitle());
        existingKnowledgePoint.setContent(knowledgePoint.getContent());
        existingKnowledgePoint.setCategory(knowledgePoint.getCategory());
        existingKnowledgePoint.setLevel(knowledgePoint.getLevel());
        existingKnowledgePoint.setExamples(knowledgePoint.getExamples());
        existingKnowledgePoint.setPractice(knowledgePoint.getPractice());
        
        return knowledgePointRepository.save(existingKnowledgePoint);
    }

    @Override
    @Transactional
    public void deleteKnowledgePoint(Long id) {
        knowledgePointRepository.deleteById(id);
    }

    @Override
    public KnowledgePoint getKnowledgePoint(Long id) {
        return knowledgePointRepository.findById(id)
            .orElseThrow(() -> new RuntimeException("知识点不存在"));
    }

    @Override
    public Page<KnowledgePoint> searchKnowledgePoints(String category, Integer level, String keyword, Pageable pageable) {
        return knowledgePointRepository.search(category, level, keyword, pageable);
    }
} 